import { defineStore } from 'pinia'
import type { BreadCrumb } from '@/types'

interface CommonStore {
  workBreadCrumbs: BreadCrumb[],
  foldStatus: boolean
}

const foldStatus = window.localStorage.getItem('fold-status')
export const commonStore = defineStore('common', {
  state: (): CommonStore => ({
    workBreadCrumbs: [],
    foldStatus: foldStatus ? Boolean(Number(foldStatus)) : false
  }),

  actions: {
    /**
     * 静态面包屑数据添加
     * @param breadCrumb
     */
    async setStaticBreadCrumb (breadCrumb: BreadCrumb) {
      const idx = this.workBreadCrumbs.findIndex(m => m.paramId === breadCrumb.paramId)
      if (idx === -1) {
        this.workBreadCrumbs.push(breadCrumb)
      } else {
        this.workBreadCrumbs = this.workBreadCrumbs.slice(0, idx + 1)
      }
    },

    updateFoldStatus () {
      this.foldStatus = !this.foldStatus
      window.localStorage.setItem('fold-status', String(Number(this.foldStatus)))
    }
  }
})
